// index.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({
  data: {
    thumb: [],
    cate: [],
    cateGoods: [],
    height: '',
    menuH: '32px',
    safeTop: `40px`,
    address: '',
    nearSkill: [],
    config: '',
    show: true,
    showPopup: false,
    // ***************
    swiperList: [],
    swiperCurrent: 0,
    serviceCardList: [
      {
        image: '/static/card1.png',
        path: '/service/newsInfo/newsInfo',
      },
      {
        image: '/static/card2.png',
        path: '/service/integralShop/integralShop',
      },
      {
        image: '/static/card3.png',
        path: '/pages/store/store',
      }
    ],
    newsList: [],
    serviceList: [],
    isStandard: true,


    // ***************
    // swiperList: [],
    // swiperCurrent: 0,
    // serviceList: [],
    // serviceIconList: [],
    // serviceCardList: [],
    // serviceAboutList: [],
    // indexIconList: [
    //   {
    //     image: '/static/index/index_icon1.png',
    //     name: '全场保障',
    //   },
    //   {
    //     image: '/static/index/index_icon2.png',
    //     name: '7×24小时服务',
    //   },
    //   {
    //     image: '/static/index/index_icon3.png',
    //     name: '不满意重做',
    //   },
    //   {
    //     image: '/static/index/index_icon4.png',
    //     name: '迟到必赔',
    //   },
    // ]

  },
  changeIdentity() {
    this.setData({
      isStandard: !this.data.isStandard
    })
  },
  goNewAll() {
    wx.navigateTo({
      url: '/service/newsInfo/newsInfo',
    })
  },
  swiperBindchange(e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  getSwiperList() {
    http.post('thumb/getlist', '', true).then(res => {
      if (res.code === 1) {
        this.setData({
          swiperList: res.data
        })
      }
    })
  },
  // getIndexCate() {
  //   http.post('category/getlist', '', true).then(res => {
  //     if (res.code === 1) {
  //       this.setData({
  //         serviceList: res.data.big_image,
  //         serviceIconList: res.data.small_image
  //       })
  //     }
  //   })
  // },
  // getAbout() {
  //   http.post('index/regard', '', true).then(res => {
  //     if (res.code === 1) {
  //       res.data[0].icon = '/static/index/service_play.png'
  //       this.setData({
  //         serviceAboutList: res.data
  //       })
  //     }
  //   })
  // },
  getNewsList() {
    const data = {
      category_id: '',
      page: 1,
      limit: 3,
    }
    http.post('article/index', data, true).then(res => {
      if (res.code === 1) {
        this.setData({
          newsList: res.data.data,
        })
      }
    })
  },
  getServiceList() {
    http.post('category/getlist', '', true).then(res => {
      if (res.code === 1) {
        console.log(res.data);
        this.setData({
          serviceList: res.data,
        })
      }
    })
  },
  goCateList(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/service/catelist/catelist?category_id=${id}`,
    })
  },
  // getMiddle() {
  //   http.post('index/middle', '', true).then(res => {
  //     if (res.code === 1) {
  //       this.setData({
  //         serviceCardList: res.data
  //       })
  //     }
  //   })
  // },
  skipDetail(e) {
    const item = e.currentTarget.dataset.item;
    util.skip('/service/servedetail/servedetail?id=' + item.goods_id)
  },
  skipTo(e) {
    const { path } = e.currentTarget.dataset;
    console.log(path, 'path...');
    if (path.indexOf('/pages') > -1) {
      wx.switchTab({
        url: path,
      })
    } else {
      util.skip(path)
    }
  },
  skipCateList(e) {
    const id = e.currentTarget.dataset.id;
    util.skip(`/service/catelist/catelist?category_id=${id}`)
  },
  skipRichText(e) {
    const id = e.currentTarget.dataset.id;
    console.log(id, '9999999999999999');
    util.skip(`/service/richText/richText?id=${id}`)
  },

  // *******************
  toPage() {
    util.skip(this.data.config.home_url)
  },
  hidePopup() {
    this.setData({
      showPopup: false
    });
  },

  tosetMealDetail() {
    wx.navigateTo({
      url: '/service/setMealDetail/setMealDetail',
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  selectAddress() {
    util.skip('/service/selectAddress/selectAddress?city=' + this.data.address.city + '&name=' + this.data.address.name)
  },
  search() {
    util.skip('/service/searchdetail/searchdetail?type=1')
  },
  nearSkill() {
    wx.switchTab({
      url: '/pages/servers/servers'
    })
  },
  cateList(e) {
    let name = e.currentTarget.dataset.item.label ? e.currentTarget.dataset.item.label : e.currentTarget.dataset.item.name
    util.skip('/service/catelist/catelist?category_id=' + e.currentTarget.dataset.item.id + '&name=' + name)
  },
  swiperTap(e) {
    let item = e.currentTarget.dataset.item;
    if (item.type == 1) {
      util.skip('/service/servedetail/servedetail?id=' + item.goods_id)
    } else if (item.type == 2) {
      util.skip('/service/info/info?id=' + item.config_text_id)
    } else if (item.type == 3) {
      util.skip(item.jump)
    }

  },
  center() {
    util.authSkip('/service/couponscenter/couponscenter')
  },
  //跳转服务者详情
  skillTap(e) {
    util.skip('/service/skilldetail/skilldetail?skill_id=' + e.currentTarget.dataset.info.id)
  },
  serviceTap(e) {
    util.skip('/service/servedetail/servedetail?id=' + e.detail.id)
  },


  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  totypedetail(e) {
    wx.navigateTo({
      url: '/service/tentype/tentype?name=' + e.currentTarget.dataset.name,
    })
  },

  go() {
    wx.navigateTo({
      url: '/service/addressmanager/addressmanager',
    })
  },


  getInfo() {
    http.post('thumb/getlist', '', true).then(res => {
      this.setData({
        thumb: res.data
      })
    })


    http.post('category/getlist', '', true).then(res => {
      this.setData({
        cate: res.data
      })
    })



  },
  getCityInfo() {
    http.get('skill/nearskill', {
      city: this.data.address.city,
      lat: this.data.address.lat,
      lng: this.data.address.lng,
      page: 1
    }, false).then(res => {
      this.setData({
        nearSkill: res.data
      })
    })
    http.post('goods/categorygoods', {
      city: this.data.address.city
    }, false).then(res => {
      this.setData({
        cateGoods: res.data
      })
    })
  },
  bindUser(bindUserId) {
    http.post('user/binding', {
      p_user_id: bindUserId
    }, false).then(res => {
      wx.removeStorageSync('bindUserId')
    }).catch(err => {
      wx.removeStorageSync('bindUserId')
    })
  },
  onLoad(options) {
    const optionScene = options.scene
    if (optionScene) {
      const sceneArr = optionScene.split('_')
      if (sceneArr[0] === 'userid') {
        const userId = sceneArr[1]
        wx.setStorageSync('bindUserId', userId)
      }
      if (sceneArr[0] === 'host') {
        const hostId = sceneArr[1]
        wx.setStorageSync('bindHostId', hostId)
      }
    }
    this.getSwiperList()
    // this.getIndexCate()
    // this.getAbout()
    this.getNewsList()
    this.getServiceList()
    // this.getMiddle()
    // *******************************
    this.setData({
      safeTop: `${app.globalData.safeTop}px`,
      menuH: `${app.globalData.menuH}px`,
    })
    this.getInfo();
    let pages = getCurrentPages()
    let curpage = pages[pages.length - 1]
    let scene = decodeURIComponent(curpage.options.scene)
    const params = {};
    scene.split('&').forEach(item => {
      const [key, value] = item.split('=');
      params[key] = value;
    });
    if (params.leader_id) {
      wx.setStorageSync('leader_id', params.leader_id)
    }
  },
  onShow() {
    const token = wx.getStorageSync('token')
    if (!token) return
    const bindUserId = wx.getStorageSync('bindUserId')
    if (bindUserId) {
      this.bindUser(bindUserId)
    }
  },
  onLoadConfig(option) {
    this.setData({
      config: app.globalData.config
    })
    if (this.data.config.home_image || this.data.config.home_content) {
      this.setData({
        showPopup: true
      })
    }
  },
  onShowAddress(option) {


    this.setData({
      address: app.globalData.address
    })
    this.getCityInfo()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '文山社会救助物质+服务'
    }
  },
  onShareTimeline() {
    return {
      title: '文山社会救助物质+服务'
    }
  }
})